<?php

namespace app\common\model\Subject;

use app\common\model\Business\Business;
use app\common\model\Subject\Subject;
use think\Model;

class Comment extends Model
{
    // 表名
    protected $name = 'subject_comment';
    // 忽略数据表不存在的字段
    protected $field = true;
    // 设置返回的结果类型
    protected $resultSetType = 'collection';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    //禁止写入时间字段
    protected $updateTime = false;
    // 软删除的字段
    protected $deleteTime = false;
    // 追加属性
    protected $append = [
        'business',
        'subject',
    ];
    public function getBusinessAttr($value,$data){
        if(!isset($data['busid']))return;
        $business = Business::where('id',$data['busid'])->select()->toArray();
        return $business?$business[0]:Business::select()->toArray()[0];
    }
    public function getSubjectAttr($value,$data){
        if(!isset($data['subid']))return;
        $subject = Subject::field('title,content,thumbs,likes,price,cateid,teacherid,createtime')->where('id',$data['subid'])->select()->toArray();
        return $subject?$subject[0]:Subject::select()->toArray()[0];
    }
    public function subject()
    {
        return $this->belongsTo(Subject::class,'subid','id',[],'left')->setEagerlyType(0);
    }
    public function business()
    {
        return $this->belongsTo(Business::class,'busid','id',[],'left')->setEagerlyType(0);
    }
    public function order()
    {
        return $this->belongsTo(Order::class,'orderid','id',[],'left')->setEagerlyType(0);
    }
}
